package com.uinnova.product.eam.service.es;

import com.uinnova.product.eam.comm.bean.WordDoc;
import com.uino.dao.AbstractESBaseDao;
import com.uino.service.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;

/**
 * 文档管理数据访问层
 *
 * @author lichong
 */
@Deprecated
@Repository
@Slf4j
public class IamsESWordDocEsDao extends AbstractESBaseDao<WordDoc, WordDoc> {
    @Override
    public String getIndex() {
        return "uino_eam_word_doc";
    }

    @Override
    public String getType() {
        return "_doc";
    }

    @PostConstruct
    public void init() {
        try {
            RestHighLevelClient clientc = getClient();
            GetIndexRequest getRequest = new GetIndexRequest();
            getRequest.indices(getFullIndexName());
            boolean existIndex = clientc.indices().exists(getRequest, RequestOptions.DEFAULT);
            if (!existIndex) {
                CreateIndexRequest createIndex = new CreateIndexRequest(getFullIndexName());
                createIndex.settings(getSetting("my-analyzer", 1));
                createIndex.mapping(getFullTypeName(), getMapping("my-analyzer"));
                clientc.indices().create(createIndex, RequestOptions.DEFAULT);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("index.mapping.total_fields.limit", 2000);
                map.put("index.max_result_window", 100000000);
                UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest();
                updateSettingsRequest.settings(map);
                clientc.indices().putSettings(updateSettingsRequest, RequestOptions.DEFAULT);
                // 仅在初始化索引时添加数据
                List<WordDoc> data = FileUtil.getData("/initdata/uino_eam_word_doc.json", WordDoc.class);
                this.saveOrUpdateBatch(data);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
